<template>
  <div class="p-4">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <el-form inline>
          <el-form-item>
            <el-button
                icon="el-icon-plus"
                type="success"
                @click="deBounceWrap(handleAdd)"
            >新增
            </el-button
            >
          </el-form-item>
        </el-form>
      </div>
      <el-table :data="data" border>
        <el-table-column label="序号" prop="id" width="150px"></el-table-column>
        <el-table-column label="展示图片" prop="picture">
          <template slot-scope="{ row }">
            <el-popover max-width="420" placement="right" trigger="hover">
              <img
                  :src="row.picture"
                  alt=""
                  style="max-width: 200px; max-height: 200px"
              />
              <img
                  slot="reference"
                  :src="row.picture"
                  alt=""
                  style="width: 50px"
              />
            </el-popover>
          </template>
        </el-table-column>
        <el-table-column label="图标" prop="icon">
          <template slot-scope="{ row }">
            <el-popover max-width="420" placement="right" trigger="hover">
              <img
                  :src="row.icon"
                  alt=""
                  style="max-width: 200px; max-height: 200px"
              />
              <img
                  slot="reference"
                  :src="row.icon"
                  alt=""
                  style="width: 50px"
              />
            </el-popover>
          </template>
        </el-table-column>
        <el-table-column label="箭" prop="arrow">
          <template slot-scope="{ row }">
            <el-popover max-width="420" placement="right" trigger="hover">
              <img
                  :src="row.arrow"
                  alt=""
                  style="max-width: 200px; max-height: 200px"
              />
              <img
                  slot="reference"
                  :src="row.arrow"
                  alt=""
                  style="width: 50px"
              />
            </el-popover>
          </template>
        </el-table-column>
        <el-table-column label="名称" prop="name"></el-table-column>
        <el-table-column label="排序" prop="level"></el-table-column>
        <el-table-column label="价格" prop="price"></el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
                icon="el-icon-edit"
                plain
                size="mini"
                type="primary"
                @click="deBounceWrap(handleEdit, scope.row)"
            >编辑
            </el-button
            >
            <el-button
                icon="el-icon-delete"
                plain
                size="mini"
                type="danger"
                @click="deBounceWrap(handleDel, scope.row)"
            >删除
            </el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <cus-pagination
          :limit.sync="listQuery.page_size"
          :page.sync="listQuery.page"
          :total="total"
          @pagination="getList"
      />
    </el-card>
    <el-dialog
        :before-close="handleClose"
        :close-on-click-modal="false"
        :title="titleOptions[title]"
        :visible.sync="dialogVisible"
        width="40%"
        @close="handleClear"
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="道具名称" label-width="120px" prop="prop_name">
          <el-input
              v-model="form.prop_name"
              placeholder="请输入道具名称"
          ></el-input>
        </el-form-item>
        <el-form-item label="图片" label-width="120px" prop="picture">
          <el-upload
              :http-request="customizeUpload"
              :on-success="handleSuccess1"
              :show-file-list="false"
              action=""
              class="avatar-uploader"

          >
            <img v-if="form.picture" :src="form.picture" class="avatar"/>
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
        <el-form-item label="图标" label-width="120px" prop="icon">
          <el-upload
              :http-request="customizeUpload"
              :on-success="handleSuccess2"
              :show-file-list="false"
              action=""
              class="avatar-uploader"

          >
            <img v-if="form.icon" :src="form.icon" class="avatar"/>
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
        <el-form-item label="箭" label-width="120px" prop="arrow">
          <el-upload
              :http-request="customizeUpload"
              :on-success="handleSuccess3"
              :show-file-list="false"
              action=""
              class="avatar-uploader"

          >
            <img v-if="form.arrow" :src="form.arrow" class="avatar"/>
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>

        <el-form-item label="排序" label-width="120px" prop="level">
          <el-input-number
              v-model.number="form.level"
              :min="0"
              placeholder="请输入排序"
          ></el-input-number>
        </el-form-item>
        <el-form-item label="价格" label-width="120px" prop="price">
          <el-input
              v-model.number="form.price"
              onkeypress="if(event.keyCode === 32)event.returnValue = false;"
              placeholder="请输入价格"
              type="number"
          ></el-input>
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer" style="text-align: center">
        <el-button plain type="primary" @click="handleClose">返回</el-button>
        <el-button type="primary" @click="deBounceWrap(handleSumbit, 'form')"
        >提交
        </el-button
        >
      </div>
    </el-dialog>

    <!-- 删除弹框 -->
    <el-dialog
        :visible.sync="deleteDialogVisible"
        title="确定删除吗?"
        width="435px"
    >
      <span style=""
      ><em style="font-style:normal;color:red">删除后不可恢复</em
      >，你还要继续吗？</span
      >
      <div slot="footer" class="dialog-footer">
        <el-button @click="deleteDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="confirmDelete">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>


import { cupidAdd, cupidDel, cupidEdit, cupidPropList } from '@/api/blindDateRoom/cupidConfiguration'

export default {
  data() {
    return {

      data: [],
      listQuery: {
        page: 1,
        page_size: 10
      },
      total: 0,
      titleOptions: {
        add: '新增',
        edit: '编辑'
      },
      title: 'add',
      rowData: {},
      form: {
        prop_name: undefined,
        picture: undefined,
        level: undefined,
        price: undefined,
        icon: undefined, //图标
        arrow: undefined // 箭 动图
      },
      rules: {
        prop_name: [
          { required: true, message: '请输入道具名称', trigger: 'blur' },
          { max: 20, message: '道具名称最多不能超过20字符' }
        ],
        picture: [
          { required: true, message: '请选择上传展示图片', trigger: 'change' }
        ],
        icon: [
          { required: true, message: '请选择上传图标', trigger: 'change' }
        ],
        arrow: [{ required: true, message: '请选择上传箭', trigger: 'change' }],
        level: [{ required: true, message: '请输入排序', trigger: 'blur' }],
        price: [{ required: true, message: '请输入价格', trigger: 'blur' }]
      },
      dialogVisible: false,
      //删除
      deleteDialogVisible: false,
      dataIds: undefined
    }
  },
  created() {
    this.getList()
  },
  methods: {
    //列表
    getList() {
      cupidPropList(this.listQuery).then(res => {
        this.data = res.data.data
        this.total = res.data.total
      })
    },
    handleSuccess1() {
      this.form.picture = this.ossLastImgUrl
    },
    handleSuccess2() {
      this.form.icon = this.ossLastImgUrl
    },
    handleSuccess3() {
      this.form.arrow = this.ossLastImgUrl
    },
    //新增
    handleAdd() {
      this.title = 'add'
      this.dialogVisible = true
    },

    //编辑
    handleEdit(row) {
      this.title = 'edit'
      this.rowData = row
      this.form.prop_name = this.rowData.name

      this.form.picture = this.rowData.picture
      this.form.level = this.rowData.level
      this.form.price = this.rowData.price
      this.form.icon = this.rowData.icon
      this.form.arrow = this.rowData.arrow
      this.dialogVisible = true
    },
    handleSumbit() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.title === 'add') {
            let data = {
              prop_name: this.form.prop_name,
              picture: this.form.picture,
              level: this.form.level,
              price: this.form.price,
              arrow: this.form.arrow,
              icon: this.form.icon
            }
            cupidAdd(data).then(res => {
              if (res.code === 200) {
                this.$message.success('提交成功')
                this.getList()
                this.dialogVisible = false
              } else {
              }
            })
          }
          if (this.title === 'edit') {
            let data = {
              id: this.rowData.id,
              prop_name: this.form.prop_name,
              picture: this.form.picture,
              level: this.form.level,
              price: this.form.price,
              arrow: this.form.arrow,
              icon: this.form.icon
            }
            cupidEdit(data).then(res => {
              if (res.code === 200) {
                this.$message.success('编辑成功')
                this.getList()
                this.dialogVisible = false
              }
            })
          }
        } else {
          return false
        }
      })
    },
    //清空表单
    handleClose() {
      this.dialogVisible = false
    },
    handleClear() {
      this.$refs['form'].resetFields()
      this.form = {
        prop_name: undefined,
        picture: undefined,
        level: undefined,
        price: undefined,
        icon: undefined, //图标
        arrow: undefined // 箭 动图
      }
    },
    //删除
    handleDel(row) {
      this.dataIds = { ids: `${row.id}` }
      this.deleteDialogVisible = true
    },
    // 确定删除
    confirmDelete() {
      cupidDel(this.dataIds).then(response => {
        if (response.code === 200) {
          this.getList()
          this.deleteDialogVisible = false
          this.$notify({
            type: 'success',
            message: '操作成功'
          })
        }
      })
    }
  }
}
</script>
<style lang="scss" scoped>
::v-deep.avatar-uploader .el-upload {
  border        : 1px dashed #d9d9d9;
  border-radius : 6px;
  cursor        : pointer;
  position      : relative;
  overflow      : hidden;
}

.avatar-uploader .el-upload:hover {
  border-color : #409eff;
}

.avatar-uploader-icon {
  font-size   : 28px;
  color       : #8c939d;
  width       : 100px;
  height      : 100px;
  line-height : 100px;
  text-align  : center;
}

.avatar {
  width   : 100px;
  height  : 100px;
  display : block;
}
</style>
